#include <LargerUnitStreamOutput.h>
#include <Unit.h>
#include <LengthUnit.h>
#include <Length.h>

namespace
{

template <typename Measurement, typename UnitType>
void convertToLargerUnit(const Measurement& measurement, const UnitType* refUnit, std::ostringstream& os)
{
    os << measurement / Measurement(1, refUnit);

    Measurement remainder = measurement % Measurement(1, refUnit);
    if (not os.str().empty() && not remainder.empty())
        os << " ";

    if (refUnit->nextUnit() != 0)
        convertToLargerUnit(remainder, refUnit->nextUnit(), os);
}

}

void LargerUnitStreamOutput::output(unsigned int amount, const Unit* unit, std::ostringstream& os) const
{
    convertToLargerUnit(Length(amount, FEET), MILE, os);
}
